import { Component, OnInit } from '@angular/core';
import { Router } from "@angular/router"
import { HttpClient, HttpHeaders } from "@angular/common/http"

@Component({
  selector: 'app-comtact-list',
  templateUrl: './comtact-list.component.html',
  styleUrls: ['./comtact-list.component.css']
})
export class ComtactListComponent implements OnInit {

  constructor(
    private router: Router,
    private http: HttpClient
  ) { }

  //定义一个联系人信息数组
  public contacts: any = []

  ngOnInit() {
    this.getContact()
  }

  getContact() {
    const token = window.localStorage.getItem('auth_token')
    this.http.get("http://localhost:3000/contacts")
      .toPromise()
      .then(data => {
        console.log(data)
        this.contacts = data
      }).catch(err => {
        console.log(err)
      })
  }

  deleteContactById(id, even) {
    even.preventDefault();//取消默认样式
    console.log(id)
    this.http.delete(`http://localhost:3000/contacts/${id}`)
      .toPromise()
      .then(data => {
        // console.log(data)
        this.getContact()
      }).catch(err => {
        console.log(err)
      })
  }

}


